
/**		
 * 
 *	uk.co.lmfm.guppy.view.components.menu.MenuView
 *	
 *	@version 1.00 | Jan 13, 2010
 *	@author Justin Windle
 *  
 **/
 
package uk.co.lmfm.guppy.view.components.menu 
{
	import uk.co.lmfm.guppy.view.components.events.MenuButtonEvent;

	import flash.utils.Dictionary;
	import flash.display.Sprite;

	import com.bit101.components.PushButton;

	import flash.events.MouseEvent;

	import uk.co.lmfm.guppy.view.components.core.AbstractView;

	/**
	 * MenuView
	 */
	public class MenuView extends AbstractView 
	{
		//	----------------------------------------------------------------
		//	PRIVATE MEMBERS
		//	----------------------------------------------------------------

		private var _enabled : Boolean = true;
		private var _buttons : Sprite = new Sprite();
		private var _index : Dictionary = new Dictionary();

		//	----------------------------------------------------------------
		//	CONSTRUCTOR
		//	----------------------------------------------------------------

		public function MenuView()
		{
			super();
			addChild(_buttons);
		}

		//	----------------------------------------------------------------
		//	PUBLIC METHODS
		//	----------------------------------------------------------------

		public function addView(name : String, label : String) : void
		{
			var button : PushButton = new PushButton();
			
			button.x = _buttons.numChildren * (button.width + 4);
			button.label = label;
			
			_buttons.addChild(button);
			_index[button] = name;
			
			enabled = enabled;
		}

		//	----------------------------------------------------------------
		//	PROTECTED METHODS
		//	----------------------------------------------------------------

		override protected function resize() : void
		{
			super.resize();
			
			x = 10;
			y = stage.stageHeight - 30;
			
			graphics.clear();
			graphics.lineStyle(0, 0xCCCCCC);
			graphics.moveTo(-x, -10);			graphics.lineTo(stage.stageWidth - x, -10);
		}

		//	----------------------------------------------------------------
		//	EVENT HANDLERS
		//	----------------------------------------------------------------

		private function onButtonClicked(event : MouseEvent) : void
		{
			dispatchEvent(new MenuButtonEvent(MenuButtonEvent.VIEW_SELECTED, _index[event.currentTarget]));
		}

		//	----------------------------------------------------------------
		//	PUBLIC ACCESSORS
		//	----------------------------------------------------------------

		public function get enabled() : Boolean
		{
			return _enabled;
		}

		public function set enabled(value : Boolean) : void
		{
			_enabled = value;
			
			var button : PushButton;
			
			for (var i : int = 0;i < _buttons.numChildren;i++)
			{
				button = _buttons.getChildAt(i) as PushButton;
				
				if(_enabled) button.addEventListener(MouseEvent.CLICK, onButtonClicked);
				else button.removeEventListener(MouseEvent.CLICK, onButtonClicked);
				
				button.alpha = _enabled ? 1.0 : 0.5;
				button.buttonMode = _enabled;
			}
		}
	}
}
